<?php
class ReservationsController extends AppController {
    public $name    = 'Reservations';
    public $helpers = array('Form');
    public $scaffold;

    public function rest_check_rooms ($ext) {
        if (!$params = $this->rest_input($ext)) {
            return;
        }

        $this->loadModel('RoomType');
        $room = $this->RoomType->availability($params['type'], array($params['checkin'], $params['checkout']));

        if ($room) { // if room available
            $this->loadModel('Rate');
            $child = isset($params['child']) ? $params['child'] : 0;
            $rates = $this->Rate->rateDetails($params['hotel_id'], $params['type'], $params['checkin'], $params['checkout'], $params['adult'], $params['child']);

            $rooms = array();
            foreach ($room as $i=>$v) {
                $rooms['room'][$i] = $v;
            }

            $resp['response'] = array(
                'status'  => 1,
                'message' => __('Reserve room(s) is available', true),
                'rooms'   => $rooms,
                'rates'   => $rates
            );
        } else {
            $resp['response'] = array(
                'status'  => 0,
                'message' => __('Reserve room(s) is unavailable', true)
            );
        }

        $this->rest_render($ext, $resp);
    } // rest_check_rooms

    public function rest_reserve ($ext) {
        if (!$params = $this->rest_input($ext)) {
            return;
        }

        $resp = $this->_reserve($params, true);

        $this->rest_render($ext, $resp);
    } // rest_reserve


    public function listing () {
        $json = $this->Reservation->reserveList($this->params['form']);
        $this->response($json);
    } // listing

    public function save () {
        $resp = $this->_reserve($this->params['form']);

        if ($resp['status'] == false) {
            $this->response($resp);
            return;
        }

        if (!$this->Reservation->saveAll($this->data)) { // error when saving data
            $mesg = empty($this->params['form']['id']) ? __('Fail insert record to database at this time, please try again.', true) : __('Fail update record to database at this time, please try again.', true);
            $err  = ($this->error) ? $this->error : $mesg;
            $resp = array(
                'status' => false,
                'mesg'   => $err
            );

            $this->_render($resp);
            return;
        }

        if (isset($data['rate_code_id'])) {
            $mesg = empty($form['id']) ? __('New reservation has been created successfully!',true) : __('Reservation has been updated successfully!',true);
        } else {
            $mesg = empty($form['id']) ? __('New walk-in folio has been created successfully!',true) : __('Walk-in folio has been updated successfully!',true);
        }

        $resp = array(
            'status' => true,
            'mesg'   => $mesg
        );

        $this->response($resp);
    } // save


    private function _reserve ($input, $rest=false) {
        $guest = array('title', 'firstname', 'lastname', 'street', 'city', 'state', 'zip_code', 'country', 'phone', 'email', 'notes');
        $resrv = array('check-in_date', 'nights', 'check-out_date', 'rooms', 'adult', 'child', 'room_type_id', 'discount');
        $stay  = array('');

        if (!$input || count($input) == 0) { // check if data exist
            return false;
        }

        if (in_array('guest_id', $input) && $input['guest_id']) {
            /*$this->loadModel('Guest');
            $guest = $this->Guest->find('first', array(
                'condition' => array('Guest.id' => $input['guest_id']),
                'recursive' => -1
            ));*/
        }

        if (empty($input['room_number'])) { // auto assign room

        }

        foreach ($input as $k=>$v) {
            if (empty($v) && $v != '0') {
                $v = null;
            }

            if (in_array($k, $guest)) {
                $data['Guest'][$k] = $v;
            } else if (in_array($k, $resrv)) {
                $data['Reservation'][$k] = $v;
            }
        }

        $this->data = $data;
        $this->Reservation->set($this->data);

        if (!$this->Reservation->validates()) { // invalid data found!
            $errors = array();
            foreach ($this->Reservation->invalidFields() as $k=>$v) {
                if ($rest) {
                    $errors['error'][] = array(
                        'fieldname' => $k,
                        'message'   => $v
                    );
                } else {
                    $errors[] = array(
                        'f' => $k,
                        'm' => $v
                    );
                }
            }

            if ($rest) {
                $resp = array(
                    'response' => array(
                        'status'  => -1,
                        'message' => __('Invalid fields found, please verify the fields pointed out and try again!', true),
                        'errors'  => $errors
                    )
                );
            } else {
                $resp = array(
                    'status' => false,
                    'mesg'   => __('Please enter the details in the fields pointed out above and try again!', true),
                    'error'  => $errors
                );
            }

            return $resp;
        }
    } // _reserve

    private function _render ($data) {
        $this->response($data);
    } // _render
} // ReservationsController
?>